#include "platform_config.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "myjni.h"
#include <pthread.h>
//#define LOGE(...)
pthread_mutex_t mutex_offline_data = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_gps_data = PTHREAD_MUTEX_INITIALIZER;
/*离线数据存储格式,链表
*|_head(sizeof(u32))_|_tail(sizeof(u32))_|_unused(48bytes)_|_data0(47bytes)_|_data1(47bytes)_|- - - -_|dataN(47bytes)_|
*
*/
extern int gps_fd, area_fd;

static int area_data_head = -1, area_data_tail = -1;
void save_area_data(char *p) {
    int ret;

    if (!p)
        return;
    //calAreaAndWight(p, area, areaRate, pesticide, seconds);
    pthread_mutex_lock(&mutex_offline_data);
    if (area_data_head == -1) {
        ret = blockRead(area_fd, 0, (void *)&area_data_head, sizeof(area_data_head));
        if (ret) {
            goto out;
        }
    }
    if (area_data_head > AREA_DATA_LEN || area_data_head < 0)
        goto out;
    ret = blockWrite(area_fd, area_data_head*47+64, p, 47);
    if (ret) {
        goto out;
    }
    area_data_head++;
    LOGE("%s head=%d", __func__, area_data_head);
    if (area_data_head >= AREA_DATA_LEN) {//溢出
        area_data_head = 0;
    }
    write_gps_info("保存离线面积");
    blockWrite(area_fd, 0, (void *)&area_data_head, sizeof(area_data_head));
out:
    pthread_mutex_unlock(&mutex_offline_data);
}


int read_area_data(char *data) {
    int ret = -1;

    pthread_mutex_lock(&mutex_offline_data);
    if (area_data_head == -1) {//只读取一次,以后保存为全局变量
        ret = blockRead(area_fd, 0, (void *)&area_data_head, sizeof(area_data_head));
        if (ret) {
            goto out;
        }
        ret = blockRead(area_fd, sizeof(area_data_head), (void *)&area_data_tail, sizeof(area_data_tail));
        if (ret) {
            goto out;
        }
    }
    if (area_data_head == area_data_tail) {
        ret = -1;
        goto out;
    }
    if (area_data_head > AREA_DATA_LEN || area_data_tail > AREA_DATA_LEN || area_data_head < 0 || area_data_tail < 0) {
        LOGE("read离线数据mu_chan.bin错误，清零");
        area_data_tail = 0;
        area_data_head = 0;
        blockWrite(area_fd, 0, (void *)&area_data_head, sizeof(area_data_head));
        blockWrite(area_fd, sizeof(area_data_head), (void *)&area_data_tail, sizeof(area_data_tail));
        ret = -1;
        goto out;
    }
    LOGE("%s head=%d, tail=%d", __func__, area_data_head, area_data_tail);
    ret = blockRead(area_fd, area_data_tail*47+64, data, 47);
    LOGE("未上传数据:%d条", area_data_head > area_data_tail ? (area_data_head - area_data_tail) : (AREA_DATA_LEN - area_data_tail + area_data_head));
    
out:
    pthread_mutex_unlock(&mutex_offline_data);
    return ret;
}


static int gps_data_count = 0;
void save_gps_data(stru_GPSPOINT *data) {
    int ret;

    if (gps_data_count >= GPS_MAX_DATA-1) {//溢出
        return ;
    }
    ret = blockWrite(gps_fd, gps_data_count*sizeof(stru_GPSPOINT)+64, (void *)data, sizeof(stru_GPSPOINT));
    if (ret) {
        return;
    }
    gps_data_count++;
    blockWrite(gps_fd, 0, (void *)&gps_data_count, sizeof(int));
}



inline unsigned int read_gps_data(stru_GPSPOINT *data) {
    unsigned int data_head = 0;
    unsigned int i, ret = 0;

    blockRead(gps_fd, 0, &data_head, sizeof(data_head));
    if (data_head > GPS_MAX_DATA) {
        LOGE("gps离线数据错误，删除");
        clean_gps_data();
    } else if (data_head > 0) {
        ret = blockRead(gps_fd, 64, data, sizeof(stru_GPSPOINT)*data_head) / sizeof(stru_GPSPOINT);
    }
    return ret;
}

inline void clean_gps_data() {
    unsigned int data_head;

    gps_data_count = 0;
    data_head = 0;
    blockWrite(gps_fd, 0, (void *)&data_head, sizeof(data_head));
}


void update_area_data_tail() {
    int ret;

    pthread_mutex_lock(&mutex_offline_data);
    if (area_data_head == -1) {
        ret = blockRead(area_fd, 0, (void *)&area_data_head, sizeof(area_data_head));
        if (ret)
            goto out;
        ret = blockRead(area_fd, sizeof(area_data_head), (void *)&area_data_tail, sizeof(area_data_tail));
        if (ret)
            goto out;
    }
    if (area_data_head == area_data_tail) {
        goto out;
    }
    area_data_tail++;
    LOGE("%s head=%d, tail=%d", __func__, area_data_head, area_data_tail);
    if (area_data_tail >= AREA_DATA_LEN) {//溢出
        area_data_tail = 0;
    }
    blockWrite(area_fd, sizeof(area_data_head), (void *)&area_data_tail, sizeof(area_data_tail));
out:
    pthread_mutex_unlock(&mutex_offline_data);

}
